package com.ghck.message.util;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;

import com.ghck.message.service.EasemobService;

public class AcceptHeaderHttpRequestInterceptor implements ClientHttpRequestInterceptor {  

	@Override  
	public ClientHttpResponse intercept(HttpRequest request, byte[] body,  
			ClientHttpRequestExecution execution) throws IOException {  
		EasemobService service = SpringContextUtil.getBean(EasemobService.class);
		HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);  
		if(StringUtils.equalsIgnoreCase(request.getMethod().name(),HttpMethod.GET.name())){
			requestWrapper.getHeaders().add(HttpHeaders.AUTHORIZATION, service.getAuthorization());
		}
		return execution.execute(requestWrapper, body);  
	}  
} 